﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

using System.Text;
using Scheduling.Models.Interfaces;


namespace Scheduling.Models.Helpers
{
    public class SchedulingHelper
    {
        /// <summary>
        /// Returns a list of bookable start times. 
        /// </summary>
        /// <returns>A list of SelectListItems. </returns>
        public static List<SelectListItem> GenerateBookingStartTimes()
        {
            return GenerateBookingTimes(15, 0);
        }

        /// <summary>
        /// Returns a list of bookable end times. 
        /// </summary>
        /// <returns>A list of SelectListItems. </returns>
        public static List<SelectListItem> GenerateBookingEndTimes()
        {
            return GenerateBookingTimes(0, 2);
        }


        /// <summary>
        /// Returns the week number from a specified date. 
        /// </summary>
        /// <param name="date">The date you want the week number retrieved from. </param>
        /// <returns>The week number. </returns>
        public static int GetWeekFromDate(DateTime date)
        {
            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;

            Calendar cal = dfi.Calendar;

            return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek); 
        }

        public static int GetPeriodLength(int Year, int Period)
        {
            DateTime[] periods = GetPeriodStartEndDate(Year, Period);
            return (int)(periods[0] - periods[1]).TotalDays;
        }

        public static DateTime[] GetPeriodStartEndDate(int Year, int Period)
        {
            switch (Period)
            {
                case 1: return new DateTime[] { new DateTime(Year, 1, 23), new DateTime(Year, 4, 1) };
                case 2: return new DateTime[] { new DateTime(Year, 4, 2), new DateTime(Year, 6, 10) };
                case 3: return new DateTime[] { new DateTime(Year, 6, 11), new DateTime(Year, 8, 19) };
                case 4: return new DateTime[] { new DateTime(Year, 8, 29), new DateTime(Year, 11, 6) };
                case 5: return new DateTime[] { new DateTime(Year, 11, 7), new DateTime(Year + 1, 1, 22) };
                default: return null;
            }
        }

        /// <summary>
        /// Generates an autocomplete list of all rooms in the database. 
        /// </summary>
        /// <returns>A string containing a list of rooms separated by commas. </returns>
        public static string GenerateRoomAutocomplete()
        {
            StringBuilder result = new StringBuilder();
            var roomLoader = IoC.Resolve<IRoomLoader>();
            return string.Join(",", roomLoader.GetRooms().Select(u => u.ID));
        }

        /// <summary>
        /// Generates an autocomplete list of all CourseInstances in the database. 
        /// </summary>
        /// <returns>A string containing a list of CourseInstances separated by commas. </returns>
        public static string GenerateCourseInstanceAutocomplete()
        {
            var courseInstanceLoader = IoC.Resolve<ICourseInstanceLoader>();
            string result = "";
            foreach (var item in courseInstanceLoader.GetCourseInstances())
            {
                result+=item.Course.ID+" ("+item.ID+" year:"+item.Year+" period:"+item.Period+"),";
            }
            result = result.Substring(0, result.Length - 1);
            return result;
        }

        public static List<Booking> SortByStartDate(List<Booking> result)
        {
            

            result.Sort(delegate(Booking b1, Booking b2)
            {
                return b1.StartDate.CompareTo(b2.StartDate);
            });
            return result;
        }


        public static List<SelectListItem> GenerateDepartmentList(string DefaultDepartment = "Faculty of Language")
        {
            var departmentLoader = IoC.Resolve<IDepartmentLoader>();
            var selectList = new List<SelectListItem>();
            foreach (Department d in departmentLoader.GetDepartments())
            {

                var item = new SelectListItem() { Text = d.Name, Value = d.ID.ToString() };
                if (d.Name == DefaultDepartment)
                    selectList.Insert(0, item);
                else
                    selectList.Add(item);
            }
            return selectList;
        }

        public static List<SelectListItem> GenerateUserGroupList(string DefaultGroup = "Users")
        {   
            var usergrouploader = IoC.Resolve<IUserGroupLoader>();
            var selectList = new List<SelectListItem>();
            foreach (UserGroup d in usergrouploader.GetUserGroups())
            {
                var item = new SelectListItem() { Text = d.Name, Value = d.ID.ToString()};
                if (d.Name == DefaultGroup)
                    selectList.Insert(0,item);
                else
                    selectList.Add(item);
            }
            return selectList;
        }

        /// <summary>
        /// Generating a list of bookable times for a SelectList
        /// </summary>
        /// <param name="MinuteShift">The number of minutes to shift every time. </param>
        /// <param name="HourShift">The number of hours to shift in the start. </param>
        /// <returns></returns>
        private static List<SelectListItem> GenerateBookingTimes(int MinuteShift, int HourShift)
        {
            var timeList = new List<SelectListItem>();
            for (int i = 0; i < 24; i++)
            {
                if ((i % 2 == 0 && i < 12 + HourShift) || (i % 2 == 1 && i > 12 + HourShift))
                    timeList.Add(new SelectListItem() { Text = string.Format("{0:00}:{1:00}", i, MinuteShift) });

            }
            return timeList;
        }
    }
}